package com.songjia.stream.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.core.MessageSource;
import org.springframework.messaging.support.GenericMessage;

/**
 * 
 * 
 * @Description: 自定义消息生产者
 *
 * @author: songjia
 * @date: 2018年3月27日 上午11:02:28
 */
@EnableBinding(SendToBinderReverse.class)
public class SendToSender {

	private static Logger logger = LoggerFactory.getLogger(SendToSender.class);

	@Value("${server.port}")
	private String port;

	/**
	 * 每隔5秒向“output”通道中发送一条消息
	 * 
	 * @return
	 */
	@Bean
	@InboundChannelAdapter(value = SendToBinderReverse.OUTPUT, poller = @Poller(fixedDelay = "5000"))
	public MessageSource<String> timerMessageSource() {
		logger.info("port: {}", port);
		return () -> new GenericMessage<>("{\"name\":\"didi\", \"age\":30}");
	}
	
	/**
	 * 处理消费者消费消息后传递回来的回调消息
	 * 
	 * @param callback
	 */
	@StreamListener(SendToBinderReverse.INPUT) // 监听input通道
    public void callback(String callback) {
        logger.info("接到消息反馈: {}", callback);
    }
}
